package com.yanqu.road.logic.activity.jieshi.logic;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.enums.activity.jieshi.eJieShiPositionType;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 灯火街市地图数据
 */
public class JieShiMapLogic {

    /**
     * 地图数据，找客户端要
     */
    public static String MAP_PARAM = "{\"npcPos1\":[101,86,73,60,49,94],\"sign1\":[[0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,8,8,8,8,8,4,4,4,4,4,4,4,0],[0,8,8,4,4,4,4,1,1,1,1,1,4,0],[0,8,4,4,1,1,1,1,4,4,4,1,4,0],[0,4,4,1,1,4,4,4,4,4,4,1,4,0],[0,4,1,1,4,4,4,4,4,4,1,1,4,0],[0,4,1,4,3,3,4,4,1,1,1,4,4,0],[0,4,1,1,3,3,0,4,1,4,4,4,8,0],[0,4,3,1,1,1,2,5,1,4,4,8,8,0],[0,8,3,3,3,3,0,4,4,4,8,8,8,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0]],\"path1\":[118,117,116,115,101,100,86,72,73,59,60,46,47,48,49,35,36,37,38,39,53,67,81,80,94,93,92,106,120,119],\"npcPos2\":[101,72,45,62,52,93],\"sign2\":[[0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,8,8,8,8,8,8,4,4,4,4,4,4,0],[0,4,4,4,4,8,8,1,1,1,1,4,4,0],[0,4,1,1,1,1,4,1,4,4,1,1,4,0],[0,4,1,4,4,1,1,1,4,4,4,1,4,0],[0,4,1,4,4,4,4,4,4,4,4,1,4,0],[0,4,1,4,3,3,4,4,4,1,1,1,4,0],[0,3,1,1,3,3,0,1,1,1,4,4,8,0],[0,3,3,1,3,1,2,5,4,4,4,8,8,0],[0,8,3,1,1,1,0,4,4,4,8,8,8,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0]],\"path2\":[118,117,131,130,129,115,101,100,86,72,58,44,45,46,47,61,62,63,49,35,36,37,38,52,53,67,81,95,94,93,107,106,105,119]}";

    /**
     * 获取地图格子数据
     * @param mapId 地图id
     */
    public static Map<Integer, Integer> getJieShiMap(int mapId) {
        Map<Integer, Integer> map = new ConcurrentHashMap<>();
        int position = 0;
        JSONObject jsonObject = JSONObject.parseObject(MAP_PARAM);
        // 拼接一下key
        String mapKey = "sign" + mapId;
        JSONArray sign = jsonObject.getJSONArray(mapKey);
        if (sign != null) {
            for (int i = 0; i < sign.size(); i++) {
                JSONArray jsonArray = sign.getJSONArray(i);
                for (int j = 0; j < jsonArray.size(); j++) {
                    map.put(position++, jsonArray.getInteger(j));
                }
            }
        }
        return map;
    }

    /**
     * 获取地图npc位置
     * @param mapId 地图id
     */
    public static List<Integer> getJieShiNpcPos(int mapId) {
        List<Integer> list = new ArrayList<>();
        JSONObject jsonObject = JSONObject.parseObject(MAP_PARAM);
        // 拼接一下key
        String pathKey = "npcPos" + mapId;
        JSONArray path = jsonObject.getJSONArray(pathKey);
        if (path != null) {
            for (int i = 0; i < path.size(); i++) {
                list.add(path.getInteger(i));
            }
        }
        return list;
    }

    /**
     * 获取地图路径
     * @param mapId 地图id
     */
    public static List<Integer> getJieShiMapPath(int mapId) {
        List<Integer> list = new ArrayList<>();
        JSONObject jsonObject = JSONObject.parseObject(MAP_PARAM);
        // 拼接一下key
        String pathKey = "path" + mapId;
        JSONArray path = jsonObject.getJSONArray(pathKey);
        if (path != null) {
            for (int i = 0; i < path.size(); i++) {
                list.add(path.getInteger(i));
            }
        }
        return list;
    }

    /**
     * 获取地图宽度（计算坐标用）
     *
     * @param mapId 地图id
     */
    public static int getJieShiMapWidth(int mapId) {
        JSONObject jsonObject = JSONObject.parseObject(MAP_PARAM);
        // 拼接一下key
        String mapKey = "sign" + mapId;
        JSONArray sign = jsonObject.getJSONArray(mapKey);
        if (sign != null) {
            JSONArray jsonArray = sign.getJSONArray(0);
            if (jsonArray != null) {
                return jsonArray.size();
            }
        }
        return 0;
    }

    /**
     * 获取默认解锁的地块
     */
    public static List<Integer> getDefaultUnlockPosition(int mapId) {
        List<Integer> list = new ArrayList<>();
        // 添加初始默认解锁地块
        Map<Integer, Integer> jieShiMap = getJieShiMap(mapId);
        for (Map.Entry<Integer, Integer> entry : jieShiMap.entrySet()) {
            if (entry.getValue() == eJieShiPositionType.DefaultUnLock.getValue() && !list.contains(entry.getKey())) {
                list.add(entry.getKey());
            }
        }
        return list;
    }

    public static void main(String[] args) {
        System.out.println(getJieShiMap(1));
        System.out.println(getJieShiMapPath(1));
        System.out.println(getJieShiMapWidth(1));
    }

}
